Skip to content

Conversation

@sandersn
Copy link
Member

Fixes #16457 (first half). This allows the following example to get the correct contextual type of Y for the object literal with type: 'y'.

interface X {
    type: 'x';
    value: 1 | 2 | 3;
    xtra: number;
}

interface Y {
    type: 'y';
    value: 11 | 12 | 13;
    ytra: number;
}

let xy: X | Y = {
    type: 'y',
    value: 11,
    ytra: 12
};

Note that many properties look like discriminants, but aren't particularly good ones. For example, the original example from #16457 has X.value : string and Y.value: 'none' | 'some'. string | 'none' | 'some' is not a good discriminant and is not helped by this PR.

@sandersn
Copy link
Member Author

This is superceded by the "more literal types" PR.

@sandersn sandersn closed this Nov 13, 2017
@mhegazy mhegazy deleted the narrow-contextual-type-of-object-literals branch November 14, 2017 20:02
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants